<template>
	<view>
		<view class="search-bar">
			<uni-search-bar v-model="keywords" bgColor="#FFFFFF" placeholder="搜索房间号/名称" @confirm="search" />
		</view>
		<view class="list-wrap">
			<view class="item card-bg" v-for="(item) in list_data" :key="item">
				<view class="row1-wrap">
					<text class="title">{{item.roomName}}</text>
					<view style="flex: 1;"></view>
					<text class="state-green">{{item.userName || ''}} {{getUserTypeName(item)}}</text>
				</view>
				<view class="row2-wrap">
					<text class="time">{{formatTime(item)}}</text>
					<view style="flex: 1;"></view>
					<text class="auth">{{getOpenTypeName(item)}}</text>
				</view>
			</view>
			<uni-load-more status="loading" v-show="isLoading" />
		</view>
		<xw-empty v-if="!list_data.length" v-on:refresh="onRefressh"></xw-empty>
	</view>
</template>

<script>
	import cfg from '@/utils/config.js'
	export default {
		data() {
			return {
				keywords: '',
				page: 1,
				isLoading: false,
				list_data: []
			}
		},
		onPullDownRefresh() {
			this.onRefressh()
			setTimeout(function() {
				uni.stopPullDownRefresh()
			}, 2000)
		},
		onReachBottom() {
			this.page++;
			this.isLoading = true
			this.getList()
		},
		onShow() {
			this.onRefressh()
		},
		methods: {
			search() {
				this.onRefressh()
			},
			onRefressh() {
				this.page = 1
				this.getList()
			},
			getOpenTypeName(item) {
				if (item.openType == 5) {
					return '远程开门'
				} else if (item.openType == 16) {
					return '人脸认证'
				} else if (item.openType == 22) {
					return '密码认证'
				}
				return ''
			},
			getUserTypeName(item) {
				if (item.userType == 2) {
					return '(访客)'
				}
				return ''
			},
			formatTime(item) {
				return cfg.timestampToTime(item.createTime)
			},
			getList() {
				uni.request({
					url: cfg.host + 'room/open/log/page-query',
					method: 'POST',
					data: {
						'pageNo': this.page,
						'roomName': this.keywords
					},
					success: (res) => {
						if (res.data.code == 0) {
							let list = res.data.data.list || []
							if (this.page == 1) {
								this.list_data = list
							} else {
								this.list_data = this.list_data.concat(list)
							}
						}
					},
					complete: () => {
						this.isLoading = false
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.search-bar {
		position: sticky;
		top: 0;
		z-index: 10;
		background-color: #f5f5f5;
	}

	.list-wrap {
		position: relative;
		width: 100%;
		display: flex;
		flex-direction: column;
	}

	.item {
		display: flex;
		flex-direction: column;

		.row1-wrap {
			display: flex;
			flex-direction: row;

			.title {
				font-size: 30rpx;
			}

			.state-green {
				color: green;
			}

			.state-red {
				color: red;
			}
		}

		.row2-wrap {
			display: flex;
			flex-direction: row;
			margin-top: 15rpx;

			.time {
				font-size: 26rpx;
				color: #666;
			}

			.auth {
				font-size: 26rpx;
				color: #666;
			}
		}

	}
</style>